home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Naming.java < prev    next >
Text File  |  1998-09-22  |  5KB  |  184 lines

  1. /*
  2.  * @(#)Naming.java    1.5 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.rmi;
  15.  
  16. import java.rmi.registry.*;
  17. import java.net.URL;
  18. import java.net.MalformedURLException;
  19.  
  20. /**
  21.  * This is the bootstrap mechanism for obtaining references to remote
  22.  * objects based on Uniform Resource Locator (URL) syntax.  The URL
  23.  * for a remote object is specified using the usual host, port and
  24.  * name:
  25.  *<br>    rmi://host:port/name
  26.  *<br>    host = host name of registry  (defaults to current host)
  27.  *<br>    port = port number of registry (defaults to the registry port number)
  28.  *<br>  name = name for remote object
  29.  */
  30. public final class Naming {
  31.     /*
  32.      * Disallow anyone from creating one of these
  33.      */
  34.     private Naming() {}
  35.  
  36.     /**
  37.      * Returns the remote object for the URL.
  38.      * @exception RemoteException If registry could not be contacted.
  39.      * @exception NotBoundException If name is not currently bound.
  40.      */
  41.     public static Remote lookup(String name)
  42.     throws NotBoundException,
  43.         java.net.MalformedURLException,
  44.         UnknownHostException,
  45.         RemoteException
  46.     {
  47.     URL url = cleanURL(name);
  48.     Registry registry = getRegistry(url);
  49.  
  50.     String file = getName(url);
  51.     if (file == null)
  52.         return registry;
  53.     return registry.lookup(file);
  54.     }
  55.     
  56.     /**
  57.      * Binds the name to the specified remote object.
  58.      * @exception RemoteException If registry could not be contacted.
  59.      * @exception AlreadyBoundException If name is already bound.
  60.      */
  61.     public static void bind(String name, Remote obj)
  62.     throws AlreadyBoundException,
  63.         java.net.MalformedURLException,
  64.         UnknownHostException,
  65.         RemoteException
  66.     {
  67.     URL url = cleanURL(name);
  68.     Registry registry = getRegistry(url);
  69.  
  70.     if (obj == null)
  71.         throw new NullPointerException("cannot bind to null");
  72.  
  73.     registry.bind(getName(url), obj);
  74.     }
  75.     
  76.     /**
  77.      * Unbind the name.
  78.      * @exception RemoteException If registry could not be contacted.
  79.      * @exception NotBoundException If name is not currently bound.
  80.      */
  81.     public static void unbind(String name)
  82.     throws RemoteException,
  83.         NotBoundException,
  84.         java.net.MalformedURLException,
  85.         UnknownHostException
  86.     {
  87.     URL url = cleanURL(name);
  88.     Registry registry = getRegistry(url);
  89.  
  90.     registry.unbind(getName(url));
  91.     }
  92.  
  93.     /** 
  94.      * Rebind the name to a new object; replaces any existing binding.
  95.      * @exception RemoteException If registry could not be contacted.
  96.      */
  97.     public static void rebind(String name, Remote obj)
  98.     throws RemoteException,
  99.         java.net.MalformedURLException,
  100.         UnknownHostException
  101.     {
  102.     URL url = cleanURL(name);
  103.     Registry registry = getRegistry(url);
  104.  
  105.     if (obj == null)
  106.         throw new NullPointerException("cannot bind to null");
  107.  
  108.     registry.rebind(getName(url), obj);
  109.     }
  110.     
  111.     /**
  112.      * Returns an array of strings of the URLs in the registry.
  113.      * The array contains a snapshot of the names present in the registry.
  114.      * @exception RemoteException If registry could not be contacted.
  115.      */
  116.     public static String[] list(String name)
  117.     throws RemoteException,
  118.         java.net.MalformedURLException,
  119.         UnknownHostException
  120.     {
  121.     URL url = cleanURL(name);
  122.     Registry registry = getRegistry(url);
  123.  
  124.     String host = url.getHost();
  125.     int port = url.getPort();
  126.  
  127.     String prefix = "rmi:";
  128.      if (port > 0 || !host.equals(""))
  129.         prefix += "//" + host;
  130.     if (port > 0)
  131.         prefix += ":" + port;
  132.     prefix += "/";
  133.  
  134.     String[] names = registry.list();
  135.     for (int i = 0; i < names.length; i++) {
  136.         names[i] = prefix + names[i];
  137.     }
  138.     return names;
  139.     }
  140.  
  141.     /** Function to find registry from URL
  142.      */
  143.     private static Registry getRegistry(URL url)
  144.     throws RemoteException, UnknownHostException
  145.     {
  146.     String host = url.getHost();
  147.     int port = url.getPort();
  148.  
  149.     return LocateRegistry.getRegistry(host, port);
  150.     }
  151.  
  152.     /** Function to extract only the name from the URL.
  153.      */
  154.     private static String getName(URL url)
  155.     {
  156.     String name = url.getFile();
  157.     if (name == null || name.equals("/"))
  158.         return null;
  159.     return name.substring(1);
  160.     }
  161.  
  162.     /**
  163.      * Function to cleanup and create the URL.
  164.      * It checks for and removes the protocol
  165.      */
  166.     private static URL cleanURL(String name) 
  167.     throws java.net.MalformedURLException
  168.     {
  169.     URL url = new URL("file:");
  170.  
  171.     // remove the approved protocol
  172.     if (name.startsWith("rmi:"))
  173.         name = name.substring(4);
  174.  
  175.     // No protocol must remain
  176.     int colon = name.indexOf(':');
  177.     if (colon >= 0 && colon < name.indexOf('/') )
  178.         throw new java.net.MalformedURLException("Protocol should be rmi:");
  179.  
  180.     url = new URL(url, name);
  181.     return url;
  182.     }
  183. }
  184.